/**
 * 
 */
package qy.jalgotrade.utils.resamplebase;

import java.time.Instant;
import java.time.ZonedDateTime;

import qy.jalgotrade.bar.Bar;

/**
 * @author qy
 *
 */
public final class ResampleUtils {

	/**
	 * ({@link IntraDayRange}) dateTime.plusSeconds(frequency.getValue());
	 * 
	 * @param beginDateTime
	 * @param frequency
	 * @return
	 */
	public static ZonedDateTime next(ZonedDateTime beginDateTime, Bar.Frequency frequency) {

		long ts = beginDateTime.toEpochSecond();
		long slot = ts / (long) frequency.getValue();
		long slotTs = slot * frequency.getValue();
		beginDateTime = ZonedDateTime.ofInstant(Instant.ofEpochSecond(slotTs), beginDateTime.getZone());
		return beginDateTime.plusSeconds(frequency.getValue());
	}

	/**
	 * 
	 * @param dateTimeInBar
	 * @param frequency
	 * @return
	 */
	public static ZonedDateTime getBeginDateTime(ZonedDateTime dateTimeInBar, Bar.Frequency frequency) {

		long ts = dateTimeInBar.toEpochSecond();
		long slot = ts / (long) frequency.getValue();
		long slotTs = slot * frequency.getValue();
		return ZonedDateTime.ofInstant(Instant.ofEpochSecond(slotTs), dateTimeInBar.getZone());
	}

	private ResampleUtils() {

	}
}
